package com.example.testblog.web.admin;


import com.example.testblog.Dao.TypeRepository;
import com.example.testblog.entity.Type;
import com.example.testblog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin")
public class TypeController {

    @Autowired
    private TypeService typeService;

/*@GetMapping(value = "/type")
    public  String type(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model)

    {
            model.addAttribute("page",typeService.listType(pageable));
        return "admin/type";
    }*/

    @GetMapping("/type")

    public String types(@PageableDefault(size = 3,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable, Model model)
    {
        model.addAttribute("page",typeService.listType(pageable));
        System.out.println(model);
        return "admin/type";
    }

    @PostMapping("/type")
    public  String  SavePut(@Valid Type type, BindingResult result , RedirectAttributes attirbutes)
    {

        if (result.hasErrors())
        {
            return "admin/type_input";
        }
        Type type1 =typeService.saveType(type);
        if (type1 == null) {
            attirbutes.addFlashAttribute("message","新增失败");
        }
        else
        {
            attirbutes.addFlashAttribute("message" ,"新增成功");
        }


        return "redirect:/admin/type";
    }
@GetMapping("/type/{id}/delete")
    public  String TypeDelete( @PathVariable Type  id)
    {

                 typeService.deleteType(id);
        return "admin/type_input";
    }


    @GetMapping("/type/{id}/input")
    public  String TypeChange( @PathVariable Long id ,Model model)
    {
                model.addAttribute("type", typeService.getType(id));

        return "admin/type_input";
    }

     @PostMapping("/type/{id}")
    public  String TypeUpdate(@PathVariable Long id, @Valid Type type,RedirectAttributes redirectAttributes)
    {
                Type type1=typeService.updateType(id,type);

                if (type1==null)
                {
                       redirectAttributes.addFlashAttribute("message","更新失败");
                }
                else
                {
                    redirectAttributes.addFlashAttribute("message","更新成功");
                }
        return "redirect:/admin/type";
    }


    @GetMapping("/type/input")
    public String input( Model model) {
      model.addAttribute("type" ,new Type());
        return "admin/type_input";
    }


}
